package com.ruoyi.project.student.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.student.domain.Student;
import com.ruoyi.project.student.domain.param.StudentListParam;
import com.ruoyi.project.student.domain.vo.StudentListVo;
import com.ruoyi.project.student.service.StudentService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 学生信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取学生列表
     */
    @PreAuthorize("@ss.hasPermi('student:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentListParam param)
    {
        startPage();
        List<StudentListVo> list = studentService.selectStudentList(param);
        return getDataTable(list);
    }

    /**
     *
     * 增加一个学生
     */
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('student:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Student student)
    {
        boolean isSuccess;
        try {
            isSuccess=studentService.save(student);
             // i =studentService.insertStudent(student);
        }catch (Exception e){
            return  error(e.getMessage());
        }
        return success(isSuccess);
    }
    /**
     *
     * 编辑一个学生
     */
    @PreAuthorize("@ss.hasPermi('student:edit')")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Student student)
    {
        try{
            Student after =studentService.edit(student);
            return success(after);
        }catch (Exception e){
            return error(e.getMessage());
        }
    }
    /**
     *
     * 删除一个学生
     */
    @PreAuthorize("@ss.hasPermi('student:delete')")
    @PostMapping("/delete")
    public AjaxResult delete(@Validated @RequestBody Student student)
    {
        try{
            int num =studentService.delete(student);
            return success(num);
        }catch (Exception e){
            return error(e.getMessage());
        }
    }
}
